package com.tfswue.mxj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfswue.mxj.service.MyuserService;
import com.tfswue.mxj.entity.Myuser;
import com.tfswue.mxj.service.MyuserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/users")
@Tag(name="用户接口",description = "符合Restful风格的用户接口")
public class RestfulController {
    @Autowired
    private MyuserService myuserservice;
    //查询所有用户
    @Operation(summary = "查询所有用户",description = "查询所有用户")
    @GetMapping
    @ApiResponse(responseCode = "200",description = "查询所有用户返回用户list")
    public List<Myuser> getAllusers(){
        return myuserservice.list();
    }
    //查询单个用户
    @Cacheable(value = "dataCache",key = "#id")
    @GetMapping("/{id}/{name}")
    public Myuser getMyuserById(@Parameter(description = "用户的id",required = true) @PathVariable("id") Integer id,@PathVariable("name")String name){
        log.error("id:{},name:{}",id,name);
        return myuserservice.getById(id);
    }
    //分页查询
    @GetMapping("/querypage/{page}/{size}")
    public List<Myuser> querPage(@PathVariable("page") Integer page,@PathVariable("size")Integer size){
        Page<Myuser> testpage = new Page<>(page,size);
        Page<Myuser> myuserPage = myuserservice.page(testpage,null);
        return myuserPage.getRecords();
    }
    //多条件分页查询
    @GetMapping("/querylist/{page}/{size}")
    public List<Myuser> queryList(@PathVariable("page") Integer page,
                                  @PathVariable("size") Integer size,
                                  @RequestBody Myuser myuser) {
        QueryWrapper<Myuser> queryWrapper = new QueryWrapper<>();
        if (myuser.getUsername() != null) {
            queryWrapper.like("username", myuser.getUsername());
        }
        if (myuser.getPassword() != null) {
            queryWrapper.like("password", myuser.getPassword());
        }

        Page<Myuser> testpage = new Page<>(page, size);
        Page<Myuser> userPage = myuserservice.page(testpage, queryWrapper);
        return userPage.getRecords();
    }
    //添加用户
    @PostMapping
    public boolean addUser(@RequestBody Myuser myuser) {
        return myuserservice.save(myuser);
    }
    //修改用户
    @Cacheable(value = "dataCache",key = "#id")
    @PutMapping
    public boolean updateUser(@RequestBody Myuser myuser) {
        return myuserservice.updateById(myuser);
    }

    //删除用户
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable("id") Integer id) {
        return myuserservice.removeById(id);
    }
}
